//
//  ChatViewController+Keyboard.swift
//  EyeVisionForDoctors
//
//  Created by PMC on 2020/2/17.
//  Copyright © 2020 tao Tao. All rights reserved.
//

import UIKit

extension ChatViewController {
    func removeNotification() {
        NotificationCenter.default.removeObserver(self)
    }
    
    func setupNotification() {
        // 监听键盘
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidShow(_:)), name: UIResponder.keyboardDidShowNotification, object: nil)
        
        // 监听消息
        NotificationCenter.default.addObserver(self, selector: #selector(receiveMessage(_:)), name: NotiName.receiveMessage, object: nil)
        /// 发送消息失败
        NotificationCenter.default.addObserver(self,
                                               selector: #selector(sendMessageFail(_:)),
                                               name: NotiName.sendMessageFail,
                                               object: nil)
        
        // 结束咨询
        NotificationCenter.default.addObserver(self, selector: #selector(sureEndAdvisory(_:)), name: NotiName.endAdvisory, object: nil)
        
        NotificationCenter.default.addObserver(self,
                                               selector: #selector(audioPlayBegin(_:)),
                                               name: NotiName.audioPlayBegin, object: nil)
        
        NotificationCenter.default.addObserver(self,
                                               selector: #selector(audioPalyEnd(_:)),
                                               name: NotiName.audioPlayEnd, object: nil)
        NotificationCenter.default.addObserver(self,
                                               selector: #selector(sensorStateChange(_:)),
                                               name: NotiName.UIDeviceProximityState,
                                               object: nil)
    }
    
    @objc private func keyboardWillShow(_ noti: Notification) {
//        tableView.scrollToBottomAnimated(false)
        keyboardControl(noti, isShowing: true)
    }
    
    @objc private func keyboardDidShow(_ noti: Notification) {
        if let keyboardSize = noti.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? CGRect {
            _ = UIEdgeInsets(top: 0, left: 0, bottom: keyboardSize.height, right: 0)
        }
    }
    
    @objc private func keyboardWillHide(_ noti: Notification) {
        keyboardControl(noti, isShowing: false)
    }
    
    func keyboardControl(_ noti: Notification, isShowing: Bool) {
        let userInfo = noti.userInfo!
        let keyboardRect = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect
        let curve = userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as? UInt32
        
        let convertedFrame = view.convert(keyboardRect!, from: nil)
        
        var bottomSafe: CGFloat = 0.0
        if !isShowing {
            bottomSafe = bottomSafeH
        }
        
        let heightOffset = view.bounds.size.height - convertedFrame.origin.y + bottomSafe
        let options = UIView.AnimationOptions(rawValue: UInt(curve!) << 16 | UIView.AnimationOptions.beginFromCurrentState.rawValue)
        let duration = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as! TimeInterval
        
        tableView.stopScrolling()
        chatBarViewPaddingBottomConstranit?.update(offset: -heightOffset)
        
        let calc_min_height = UIScreen.main.bounds.size.height - detailView.frame.size.height - heightOffset - topSafeH - navBarH
        if calc_min_height < kChatBarViewOriginalHeight {
            if isShowing {
                tableView.snp.remakeConstraints { make in
                    //                make.top.equalTo(detailView.snp.bottom)
                    make.top.equalToSuperview()
                    make.left.right.equalToSuperview()
                    make.bottom.equalTo(chatBarView.snp.top)
                }
            } else {
                tableView.snp.remakeConstraints { make in
                    make.top.equalTo(detailView.snp.bottom)
                    make.left.right.equalToSuperview()
                    make.bottom.equalTo(chatBarView.snp.top)
                }
            }
        }
        
        UIView.animate(withDuration: duration, delay: 0, options: options, animations: {
            self.view.layoutIfNeeded()
            self.tableView.scrollBottomToLastRow()
        })
    }
}
